package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuilderController;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.pojo.tablestruct.NeedQueryParams;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.beanUtil.BeanUtils;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateController extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateController(List<NeedQueryParams> needQueryParams) {
    this.getTargetPackageAddress(needQueryParams);
  }

  public GenerateController(Map<String, List<NeedQueryParams>> needQuerymaps) {
    this.getTargetPackageAddress(needQuerymaps);
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    Map<String, List<NeedQueryParams>> needQueryMaps = null;
    for (int i = 0; i < youParameter.length; i++) {
      needQueryMaps = (Map<String, List<NeedQueryParams>>) youParameter[0];
    }
    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    for (LibaryProperties libaryProperty : libaryProperties) {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        String path = CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isControllerisLastLayer(), packagePathConfiguration.getControllerPath());
        //获取类名
        String classname = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true);
        String classnameForVo = classname + "Vo";
        String classnameForDto = classname + "Dto";
        String classnameForService = classname + "Service";
        String classnameForController = classname + "Controller";


        List<String> importPath = Arrays.asList(super.getPathAddress(classnameForVo), super.getPathAddress(classnameForDto), super.getPathAddress(classnameForService));
        List<NeedQueryParams> needQueryParams = null;
        if (needQueryMaps != null) {
          needQueryParams = needQueryMaps.get(libaryProperty.getTableName());
        }
        BuilderController builderController = new BuilderController(packgePath.toString(), importPath, classnameForService, classnameForDto, classnameForVo, classnameForController, needQueryParams);
        super.putPathAddress(classnameForController, packgePath);
        //放文件名和文件地址
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(path, classnameForController, builderController);
        packageAddress.add(filePathAndClassName);
      }
    }

    super.build(packageAddress, templateConfiguration.getControllerPath());
    return packageAddress;
  }
}
